<?php
if(!is_null($this->element->getRow())){
$peer = ShopPeer::getInstance();
$elRow = $this->element->getRow();
if ($elRow) {
	$id = $this->element->getRow()->getId();
	$select_y = $peer->select();
	$select_y->where('`id` = ?', $id);
	$map = $peer->fetchAll($select_y);
} else {
	$elRow = $peer->createRow();
	$map = array();
}
}
?>
<script src="http://api-maps.yandex.ru/1.1/index.xml?key=AFgQZk0BAAAAKn_KXgIARoeb3CWGIW13qey2LLwAAYOvGLIAAAAAAAAAAACdLqS_mBKsZ_GEIT95hoMwsRS3sQ==~AD3OZ00BAAAAlhOxdgQAdwkT0H34wTbYFN5aeLROXV6Na2QAAAAAAAAAAACC9dPPQywW1ZxikDvWXqHjz-Yyqw==~ANp3Ok4BAAAAyVoLdQIAbFnp3alffpe0PeOO6lkkv4f3dd8AAAAAAAAAAADB83atjXPDTOWr62ca2J1Lg5c0RQ==~AJh3Ok4BAAAAH87qIAQAhlJgk6i2qHzgTZu5DFyXLQle6YEAAAAAAAAAAAChRLc7cSSF3a2ZvDBRD8cl1bYYlA=="
		type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">

var map;
var mark = false;
// Создание обработчика для события window.onLoad
	YMaps.jQuery(function () {
		// Создание экземпляра карты и его привязка к созданному контейнеру
		map = new YMaps.Map(YMaps.jQuery("#YMapsID")[0]);
		$(document).ready(function(){
		<?php if(!is_null($this->element->getRow())){?>
		var city_name = $('select#city_id option:selected').text();
		var geocoder = new YMaps.Geocoder(city_name);
		YMaps.Events.observe(geocoder, geocoder.Events.Load, function (){
		var city_name = this.get(0).getGeoPoint();
		map.setCenter(city_name, 10);
		<?php }else{?>
		var city_name = 'Москва';
		var geocoder = new YMaps.Geocoder(city_name);
		YMaps.Events.observe(geocoder, geocoder.Events.Load, function (){
		var city_name = this.get(0).getGeoPoint();
		map.setCenter(city_name, 10);
		<?php }?>
		});
});

		map.addControl(new YMaps.Zoom());
		map.addControl(new YMaps.TypeControl());
		map.enableScrollZoom();
		map.addControl(new YMaps.SearchControl());

<?php if(!is_null($this->element->getRow())){
	foreach($map as $maps){ ?>

		var magazine = new YMaps.Placemark(new YMaps.GeoPoint('<?php echo $maps['lat']; ?>', '<?php echo $maps['lng']; ?>'), { style: "default#shopIcon" });
		magazine.name = '<?php echo $maps['name']; ?>';
		magazine.description ='<strong>Адрес магазина:</strong> <?php echo $maps['address']; ?> <br> <strong>Режим работы:</strong><?php echo $maps['working_hours']; ?> ';
		map.addOverlay(magazine);
<?php }

	}?>
	var toolbar = new YMaps.ToolBar();

YMaps.Events.observe(map, map.Events.Click, function (map, mEvent) {

var newGeoPoint = mEvent.getGeoPoint();

YMaps.jQuery("#image").change(function() {
YMaps.jQuery("#imagePreview").empty();
if ( YMaps.jQuery("#image").val()!="" ){
    YMaps.jQuery("#imagePreview").append("<img src=\"" + YMaps.jQuery("#image").val()  + "\" />");
}
else{
    YMaps.jQuery("#imagePreview").append("displays image here");
    }
});

YMaps.jQuery("#formpoint").show();

document.getElementById('lat').value = newGeoPoint.getLng();
document.getElementById('lng').value = newGeoPoint.getLat();

	var latn = newGeoPoint.getLat();
	var lngn = newGeoPoint.getLng();



});
	YMaps.Events.observe(map, map.Events.Click, function (map, mEvent) {
	   if(!mark){
	   mark = new YMaps.Placemark(mEvent.getGeoPoint());
		mark.description = 'Новая метка';
	   map.addOverlay(mark);
	   }else{
			map.removeOverlay(mark);
			mark = new YMaps.Placemark(mEvent.getGeoPoint());
			mark.description = 'Новая метка';
			map.addOverlay(mark);
		}
   });

});


</script>

<div id="YMapsID" style="float:left; width:600px; height:400px;"></div>

<div class="clear"></div>


<?php
$form = $this->element;
$form->clearDecorators();
$form->loadDefaultDecorators();
echo $form;

?>